package com.itheima.reggie.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {



    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //没有userId，设置一下
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断菜品或者套餐是否被点过，要处理点的数量
        Long dishId = shoppingCart.getDishId();
        //判断dishId是否为空
        //如果为空：
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(userId!=null,ShoppingCart::getUserId,userId);
        if(dishId==null){
            //那传来的就是套餐
            //看看里面是否添加过该套餐(那就查询呗)，给number赋值
            //sql:select * from shoppingcart where userId=? and setmealId=?
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart setmeal = shoppingCartService.getOne(qw);
            //判断setmeal是否为空
            if(setmeal==null){
                //为空就是没有添加过
                //那就添加到购物车呗
                shoppingCart.setNumber(1);
                shoppingCartService.save(shoppingCart);
            }else {
                shoppingCart.setNumber(setmeal.getNumber()+1);
                setmeal.setCreateTime(LocalDateTime.now());
                shoppingCartService.updateById(setmeal);
                shoppingCart=setmeal;
            }

        }else {
            //那传来的就是菜品dish
//
            qw.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart dish = shoppingCartService.getOne(qw);
            if(dish==null){
                //没有添加过，直接添加
                shoppingCart.setNumber(1);
                shoppingCartService.save(shoppingCart);

            }else {
                //添加过就直接更新
                dish.setCreateTime(LocalDateTime.now());
                dish.setNumber(dish.getNumber()+1);
                shoppingCartService.updateById(dish);
                shoppingCart=dish;

            }
        }
        return R.success(shoppingCart);
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return list!=null?R.success(list):R.error("查询失败");
    }

    /**
     * 取消购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //思路分析
        //取消购物车就是让购买数量减一呗，这个时候要查询到购物车里这个菜品或者套餐的数据
        //根据UsrId和传来的dishId/setmealId来查询
        //不能确定传来的是dishId还是setmealId，所以要进行判断
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId==null){
            //就是setmealId
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
            qw.eq(ShoppingCart::getDishId,dishId);
        }
        ShoppingCart shopping = shoppingCartService.getOne(qw);
        Integer number = shopping.getNumber();
        number--;
        shopping.setNumber(number);
        if(number>0){
            //就是有数据，直接更新
            shoppingCartService.updateById(shopping);
        }else {
            //直接在购物车中删除
            shoppingCartService.remove(qw);
        }
        return R.success(shopping);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        boolean remove = shoppingCartService.remove(qw);
        return remove?R.success("清空购物车成功"):R.error("清空购物车失败");
    }



}
